package com.ykht.proj.comsrv.handle;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.common.core.constant.SecurityConstants;
import com.ykht.proj.comsrv.constant.ComConstant;
import com.ykht.proj.comsrv.entity.VSubscribeMsg;
import com.ykht.proj.comsrv.vo.SubscribeMsg;
import com.ykht.proj.comsrv.vo.SubscribeMsgCardExpire;
import com.ykht.proj.comsrv.vo.SubscribeMsgCouponExpire;
import com.ykht.proj.coupon.api.feign.RemoteCouponLogService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 优惠券过期实现类
 */
@Slf4j
@Component(ComConstant.SUBSCRIBE_MSG_COUPON_EXPIRE)
@RefreshScope
@SuppressWarnings("all")
public class SubscribeMsgCouponExpireHandler extends AbstractSubscribeMsgHandler{

	@Resource
	private WxMaService wxMaService;

	@Value("${subscribeMsg.couponExpire.templateId:3z3L6qa5BMdbr1zHwAO6iCcz-W7Mx964X9dzwFp2l2A}")
	private String templateId;
	@Value("${subscribeMsg.couponExpire.page:/pages/merchant/coupon_detail}")
	private String page;

	@Autowired
	private RemoteCouponLogService remoteCouponLogService;

	@Override
	public VSubscribeMsg checkSend(SubscribeMsg msg) {
		SubscribeMsgCouponExpire tempMsg = (SubscribeMsgCouponExpire)msg;
		VSubscribeMsg vSubscribeMsg = new VSubscribeMsg().selectOne(Wrappers.lambdaQuery(VSubscribeMsg.class)
				.eq(VSubscribeMsg::getPersonId,tempMsg.getPersonId())
				.eq(VSubscribeMsg::getStatus,0)
				.eq(VSubscribeMsg::getSendFlag,0)
				.eq(VSubscribeMsg::getTatId,tempMsg.getCouponId())
				.eq(VSubscribeMsg::getType,msg.getType()));
		Boolean flag = false;
		if(vSubscribeMsg!=null){
			flag = remoteCouponLogService.sendMsgFlag(vSubscribeMsg.getTatId(),vSubscribeMsg.getPersonId(), SecurityConstants.FROM_IN);
		}
		return !flag?null:vSubscribeMsg;
	}

	@Override
	public WxMaSubscribeMessage transform(SubscribeMsg tempMsg) {
		SubscribeMsgCouponExpire msg = (SubscribeMsgCouponExpire)tempMsg;
		List<WxMaSubscribeMessage.Data> list = new ArrayList<>();
		list.add(new WxMaSubscribeMessage.Data("thing1",msg.getCouponName()));
		list.add(new WxMaSubscribeMessage.Data("amount2",msg.getPrice().toString()));
		list.add(new WxMaSubscribeMessage.Data("thing3",msg.getContent()));
		list.add(new WxMaSubscribeMessage.Data("time4",LocalDateTime.parse(msg.getEndTime()).format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm"))));
		return new WxMaSubscribeMessage(msg.getOpenId(),templateId,page+"?id="+msg.getCouponId(),list, WxMaConstants.MiniprogramState.FORMAL,WxMaConstants.MiniprogramLang.ZH_CN);
	}
	@Override
	public void send(Object data) throws WxErrorException {
		wxMaService.getMsgService().sendSubscribeMsg((WxMaSubscribeMessage)data);
		log.info("发送消息:{}",data);
	}

	@Override
	public void save(VSubscribeMsg vSubscribeMsg,Object data,SubscribeMsg tempMsg) {
		SubscribeMsgCouponExpire msg = (SubscribeMsgCouponExpire)tempMsg;
		vSubscribeMsg.setMsg(JSONUtil.toJsonStr(data));
		vSubscribeMsg.setPersonId(msg.getPersonId());
		vSubscribeMsg.setOpenId(msg.getOpenId());
		vSubscribeMsg.setTatId(msg.getCouponId());
		vSubscribeMsg.setType(msg.getType());
		vSubscribeMsg.setStatus(0);
		vSubscribeMsg.setSendFlag(1);
		vSubscribeMsg.setCreateTime(LocalDateTime.now());
		vSubscribeMsg.updateById();
	}


}
